<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #42b983">
    <div style="display: flex; background-color: white; width:60%; border-radius: 10px; overflow: hidden">
      <div style="flex: 1">
        <img src="../assets/img/register.jpg" alt="" style="width: 100%; height: 100%;">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" :rules="rules" ref="registerRef">
          <div style="font-size: 30px; font-weight: bold; text-align: center; margin-bottom: 25px;">用户注册
          </div>

          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user-solid" placeholder="请输入用户名"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code_confirm">
            <el-input prefix-icon="el-icon-circle-check" show-password placeholder="请确认密码"
                      v-model="user.code_confirm"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="success" style="width: 100%" @click="register">注&nbsp; &nbsp;册</el-button>
            <!--//注册还需要写入数据库-->
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已有账号？请 <span style="color: darkcyan; cursor: pointer"
                                              @click="$router.push('/login')">登录</span></div>
          </div>

        </el-form>
      </div>
    </div>


  </div>
</template>


<script>

import ValidCode from "../components/ValidCode";

export default {
  name: "Login",
  components: {
    ValidCode
  },

  data() {
    //两次密码验证
    //箭头函数
    const password_confirm = (rule, value, callback) => { //value是用户输入的图形验证码
      if (value === '') {
        callback(new Error('请确认密码'));
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback(); //图形验证通过
      }
    };

    return {
      //表单规定
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        code_confirm: {validator: password_confirm, required: true, message: '请确认密码', trigger: 'blur'}
      },

      code: '',  //验证码组件里的code,通过getCode方法更新值
      user: {
        username: '',
        password: '', //第一次输入密码
        code_confirm: '', //确认密码
      }
    }

  },

  methods: {

    register() { //注册
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/register', this.user).then(res => {
            //进入主界面
            if (res.code === '200') {
              this.$router.push('/login') //跳转登录界面
              this.$message.success("注册成功！请登录")
            } else {
              this.$message.error(res.msg)
            }
            console.log(res)
          })
        } else {
          //提示错误
        }
      });

    }
  }

}
</script>

<style scoped>

</style>